﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Device
{
    public class StringHexByteUtils
    {
        /// <summary>
        /// Hex 字符串转换为字节数组
        /// </summary>
        /// <param name="hexString">Hex字符串</param>
        /// <returns></returns>
        public static byte[] HexStringToByte(string hexString)
        {
            string[] strs = hexString.Split(' ');

            List<byte> listb = new List<byte>();

            foreach (var item in strs)
            {
                listb.Add((byte)Convert.ToInt32(item, 16));
            }
            var data = listb.ToArray();

            return data;
        }

        /// <summary>
        /// 将字节数组数据转为Hex 字符串
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <returns></returns>
        public static string ByteToHexString(byte[] bytes)
        {
            if (bytes == null) return "";

            var hex = BitConverter.ToString(bytes, 0).Replace("-", " ").ToUpper();

            return hex;
        }

        public static int byte2Int(byte[] b)
        {
            int intValue = 0;
            for (int i = 0; i < b.Length; i++)
            {
                intValue += (b[i] & 0xFF) << (8 * (3 - i));
                //System.out.print("intvalue --- "+intValue+" ");
            }
            return intValue;
        }

        /**
     * 字节转整型
     *
     * @param b 字节数组
     * @return int型
     */
        public static int hexString2Int(string hexString)
        {
            byte[] b = HexStringToByte(hexString);

            int intValue = 0;
            for (int i = 0; i < b.Length; i++)
            {
                intValue += (b[i] & 0xFF) << (8 * (3 - i));
                //System.out.print("intvalue --- "+intValue+" ");
            }
            return intValue;
        }


        public static int byte2Int_l(byte[] b)
        {
            int intValue = 0;
            for (int i = 0; i < b.Length; i++)
            {
                intValue += (b[i] & 0xFF) << (8 * (2 - i));
                //System.out.print("intvalue --- "+intValue+" ");
            }
            return intValue;
        }

        /**
     * 字节转整型
     *
     * @param b 字节数组
     * @return int型
     */
        public static int hexString2Int_l(string hexString)
        {
            byte[] b = HexStringToByte(hexString).Reverse().ToArray();
            int intValue = 0;
            for (int i = 0; i < b.Length; i++)
            {
                intValue += (b[i] & 0xFF) << (8 * (1 - i));
                //System.out.print("intvalue --- "+intValue+" ");
            }
            return intValue;
        }

        /// <summary>
        /// 将整形数值转换为Hex 字符串
        /// </summary>
        /// <param name="iNum">整形数字</param>
        /// <returns></returns>
        public static string IntegerToHexString(int iNum)
        {
            string str = iNum.ToString("X4");

            return str;
        }

        /// <summary>
        /// 将整形数值转换为Hex 字符串
        /// </summary>
        /// <param name="iNum">整形数字</param>
        /// <param name="formatString">整形数字</param>
        /// <returns></returns>
        public static string IntegerToHexString(int iNum, string formatString)
        {
            string str = iNum.ToString(formatString);

            return str;
        }

        /// <summary>
        /// 将hex字符串转换为数字
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static int HexStringToInt(string hexString)
        {
            int result = Convert.ToInt32(hexString, 16);

            return result;
        }

        /// <summary>
        /// int转byte
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] intToBytes(int value)
        {
            byte[] src = new byte[4];
            src[3] = (byte)((value >> 24) & 0xFF);
            src[2] = (byte)((value >> 16) & 0xFF);
            src[1] = (byte)((value >> 8) & 0xFF);
            src[0] = (byte)(value & 0xFF);
            return src;
        }

        /// <summary>
        /// int转byte
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] int16ToBytes(Int16 value)
        {
            byte[] src = new byte[2];
            src[1] = (byte)(value & 0xFF);
            src[0] = (byte)((value >> 8) & 0xFF);
            return src;
        }

        /// <summary>
        /// int转byte
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] Uint16ToBytes(UInt16 value)
        {
            byte[] src = new byte[2];
            src[1] = (byte)(value & 0xFF);
            src[0] = (byte)((value >> 8) & 0xFF);
            return src;
        }

        public static string FormatHexStr(string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            str = str.Replace(" ", "");
            var s = string.Join(" ", Regex.Matches(str, @"..").Cast<Match>().ToList());
            return s;
        }

        public static byte[] StringIpToBytes(string ip)
        {
            if (string.IsNullOrEmpty(ip))
                return null;
            string[] section = ip.Split(".");
            List<byte> ipByteList = new List<byte>();
            if (section.Length == 4)
            {
                foreach (var item in section)
                {
                    ipByteList.Add((byte)Convert.ToInt32(item));
                }
            }
            else
            {
                return null;
            }
            return ipByteList.ToArray();
        }

    }
}
